package com.fun.feifun.modules.business.impl;

import com.fun.feifun.base.result.Result;
import com.fun.feifun.base.utils.MD5Util;
import com.fun.feifun.base.utils.format.FormatUtil;
import com.fun.feifun.base.utils.http.HttpUtil;
import com.fun.feifun.modules.business.model.BusinessInterface;
import com.fun.feifun.modules.business.dao.BusinessInterfaceMapper;
import com.fun.feifun.modules.business.service.IBusinessInterfaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 业务供应商 服务实现类
 * </p>
 *
 * @author wkf
 * @since 2024-06-27
 */
@Service
public class BusinessInterfaceServiceImpl extends ServiceImpl<BusinessInterfaceMapper, BusinessInterface> implements IBusinessInterfaceService {

    @Override
    public Map<String, String> getbalance(BusinessInterface businessInterface, Map<String, String> param) {
        Integer interfaceType = businessInterface.getInterfaceType();
        if(!interfaceType.equals(1)){
            throw  new RuntimeException("暂未实现此类型的接口");
        }
        String url=businessInterface.getUrl();
        String user_id=param.get("userid");
        String key=param.get("key");
        String sign="";
        try {
            //md5加密
            StringBuilder signStr = new StringBuilder();
            signStr.append("userid="+user_id+"&");
            signStr.append("key="+key);
            sign = MD5Util.encrypt(signStr.toString());

            Map<String,String> map=new HashMap<>();
            map.put("userid",user_id);
            map.put("sign",sign);
            //请求
            String  responseBody= HttpUtil.odPost(url, map);
            //请求失败
            Map<String, String> responsMap = FormatUtil.convertXmlToMap(responseBody);
            responsMap.containsKey("userid");
            responsMap.containsKey("balance");
            return responsMap;
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("请求"+businessInterface.getTitle()+"接口请求失败");
        }
    }



    @Override
    public Map<String, Object> getOrderInfo(BusinessInterface businessInterface, Map<String, String> param) {
        return null;
    }

    @Override
    public Map<String, Object> placeOrder(BusinessInterface businessInterface, Map<String, String> param) {
        return null;
    }
}
